package com.Jshop.auth.service;

import com.Jshop.auth.entity.User;
import com.Jshop.auth.utils.JwtUtils;
import com.Jshop.auth.client.UserClient;
import com.Jshop.auth.config.JwtProperties;
import com.Jshop.auth.entity.UserInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AuthService {

    @Autowired
    private UserClient userClient;

    @Autowired
    private JwtProperties properties;

    public String authentication(String username, String password){
        try {
            //调用微服务，查询用户是否存在
            User user = this.userClient.queryUser(username,password);
            if (user == null){
                return null;
            }
            //用户确实存在，生成Jwt类型的token，token的签名经过RSA私钥加密
            String token = JwtUtils.generateToken(new UserInfo(user.getId(), user.getUsername()),
                    properties.getPrivateKey(), properties.getExpire());
            return token;

        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }
}
